/**
 * 主页JS
 * 2018年7月31日14:50:25
 * xwx
 * */

var indexPage = function() {

	function init() {

		var firstBack;
		mui.init({
			beforeback: function() {
				if(!firstBack) {
					mui.toast('再按一次退出应用');
					firstBack = true;
					setTimeout(function() {
						firstBack = null;
					}, 1000);
					return false;
				}
				plus.runtime.quit();
			}
		});

		mui.plusReady(function() {
			var subpages = [
				'/html/sub-home.html',
				'/html/sub-organize.html',
				'/html/sub-message.html',
				'/html/sub-my.html'
			];
			
			var tempheight = (plus.display.resolutionHeight -52) + 'px';

			var subpageStyle = {
				top: 0, //新页面顶部位置
				bottom: 51, //新页面底部位置
			};

			var self = plus.webview.currentWebview(); //当前页面
			for(var i = 0; i < subpages.length; i++) {
				var sub = plus.webview.create(subpages[i], subpages[i], subpageStyle); //当前Webview的URL地址、ID、样式。功能：创建子窗体
				sub.hide(); //功能：隐藏页面
				self.append(sub); //功能： 追加子页面,首个选项卡页面显示，其它均隐藏
			}

			window.addEventListener("myhfresh", function(e) {
				location.reload();
			});

			plus.webview.show(subpages[0]);
			var activeTab = subpages[0];

			mui("body").on("tap", ".mui-bar-tab a", function(e) { //通过.mui-bar-tap里的a元素绑定tap事件，达到批量绑定的效果
				var tagPage = this.getAttribute("href"); //getAttribute()获取属性函数

				if(activeTab != tagPage) {

					switch(tagPage) {
						/*case subpages[0]:
							var subhomePage = plus.webview.getWebviewById('/html/sub-home.html');
							if(subhomePage) {
								mui.fire(subhomePage, 'myfresh');
							}
							break;*/
						/*case subpages[1]:
							var suborganizePage = plus.webview.getWebviewById('/html/sub-organize.html');
							if(suborganizePage) {
								mui.fire(suborganizePage, 'myfresh');
							}
							break;*/
						/*case subpages[2]:
							var submessagePage = plus.webview.getWebviewById('/html/sub-message.html');
							if(submessagePage) {
								mui.fire(submessagePage, 'myfresh');
							}
							break;*/
						case subpages[3]:
							var subcmyPage = plus.webview.getWebviewById('/html/sub-my.html');
							if(subcmyPage) {
								mui.fire(subcmyPage, 'myfresh');
							}
							break;
						default:
							break;
					}

					plus.webview.show(tagPage, "fade-in", 100) //fade-in淡入，100毫秒
					activeTab = tagPage;
				}
				/*if(tagPage == activeTab) {
					return;
				}

				var self = plus.webview.currentWebview();
				var sub = plus.webview.create(tagPage, tagPage, subpageStyle);
				self.append(sub);
				//隐藏当前;    
				plus.webview.close(activeTab);
				activeTab = tagPage;*/
			})
		});
	};

	return {
		init: init
	}
}();

(function($, doc) {

	indexPage.init();

	$.plusReady(function() {
		$.oldBack = mui.back;
		var backButtonPress = 0;
		$.back = function(event) {
			backButtonPress++;
			if(backButtonPress > 1) {
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast('再按一次退出应用');
			}
			setTimeout(function() {
				backButtonPress = 0;
			}, 1000);
			return false;
		};
	});
}(mui, document));